package com.sfx.算法专题.双指针.n数之和;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-07-26
 * Time: 9:45
 */
public class Code_2_twoSum {
    /**
     * 哈希表法
     */
    static class Solution {
        public int[] twoSum(int[] nums, int target) {
            int[] res = new int[2];
            Map<Integer,Integer> map = new HashMap<>();
            for(int i =0;i<nums.length;++i) {
                if(map.containsKey(target - nums[i])) {
                    res[0] = map.get(target - nums[i]);
                    res[1] = i;
                    return res;
                }else {
                    map.put(nums[i],i);
                }
            }
            return null;
        }
    }

    /**
     * 暴力解法
     */
    static class Solution2 {
        public int[] twoSum(int[] nums, int target) {
            int n = nums.length;
            for (int i = 0; i < n; ++i) {
                for (int j = i + 1; j < n; ++j) {
                    if (nums[i] + nums[j] == target) {
                        return new int[]{i, j};
                    }
                }
            }
            return new int[0];
        }
    }
}